package numberslider;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: Dec 23, 2009
 * Time: 12:37:01 AM
 * To change this template use File | Settings | File Templates.
 */
public class CachingResultSetTableModel extends ResultSetTableModel {

    public CachingResultSetTableModel(ResultSet aResultSet) {
        super(aResultSet);
        try {
            cache = new ArrayList();
            int cols = getColumnCount();
            ResultSet rs = getResultSet();

            while (rs.next()) {
                Object[] row = new Object[cols];
                for (int j = 0; j < row.length; j++)
                    row[j] = rs.getObject(j + 1);
                cache.add(row);
            }
        }
        catch (SQLException e) {
            System.out.println("Error " + e);
        }
    }

    public Object getValueAt(int r, int c) {
        if (r < cache.size())
            return ((Object[]) cache.get(r))[c];
        else
            return null;
    }

    public int getRowCount() {
        return cache.size();
    }

    private ArrayList cache;
}

